#include "stm32f10x.h"          
#include "Function.h" 
uint16_t ADF[4],Tur,Tur_adjust = 2300,ADD1 = 0,ADD2 = 0;
uint8_t TDS[6] = {0},flag_arrows = 1,flag_sure = 0;
uint8_t Savedata[8] = {0},page_turner = 0;
float Tur_V,PH_V,PH;
short Temp,Temp_L;
uint8_t Mode_flag = 0;
void Function_Init(void)
{
	OLED_Init();
	W25Q16_Init();
	W25Q16_SectorErase(ADD1);
	AD_Init();
	TDS_Init();
	Buttonexti_Init();
	OLED_Showhello();//开机画面
	Delay_ms(500);
}
void Function_Mode(void)
{
		OLED_Showmode();
		if(flag_arrows == 1) //选择自动测量模式
		{
			OLED_ShowString (2,15,"<-");
			OLED_ShowString (3,15,"  ");
			OLED_ShowString (4,15,"  ");
		}
		else if(flag_arrows == 2) //选择元件校准模式
		{
			OLED_ShowString (3,15,"<-");
			OLED_ShowString (2,15,"  ");
			OLED_ShowString (4,15,"  ");
		}
		else if(flag_arrows == 3)//选择历史记录模式
		{
			OLED_ShowString (4,15,"<-");
			OLED_ShowString (3,15,"  ");
			OLED_ShowString (2,15,"  ");
		}
		if (flag_sure == 1&flag_arrows == 1)
		{
			OLED_Clear();
			while(1)
			{
				OLED_Showbase();//基础字符显示
				Send_Start();
				Tur_V=(float )ADF[0]/4095*3.3;
				Tur=(-865.68)*Tur_V*2+Tur_adjust;
				PH_V=(float )ADF[1]/4095*3.3;
				PH=(-5.7541)*PH_V+16.654;
				if (DS18B20_Init()==0)
				{
					DS18B20_Start();
					Temp=DS18B20_Get_Temp();
					OLED_ShowNum (3,11,Temp,1);
					Savedata[5] = Temp % 10;
					Temp=Temp/10;
					Savedata[4] = (uint8_t)Temp;
					OLED_ShowNum (3,8,Temp,2);
				}
				OLED_ShowNum (1,8,Tur,4);
				Savedata[1] = Tur;
				Savedata[0] = Tur>>8;
				if (PH<14)
				{
					OLED_ShowNum (2,8,PH,2);
					OLED_ShowNum (2,11,(uint16_t)(PH*100)%100,2);
					Savedata[3] = (uint16_t)(PH*100)%100;
					Savedata[2] = (uint8_t)PH;
				}
				OLED_ShowNum (4,8,TDS[1]*16*16+TDS[2],4);
				Savedata[6] = TDS[1];
				Savedata[7] = TDS[2];
				W25Q16_PageProgram(ADD1, Savedata, 8);
				ADD1 += 8;
				if (flag_sure == 2) break;
				Delay_ms(1000);
				if (flag_sure == 2) break;
			}
			OLED_Clear(); 
		}
		else if(flag_sure == 1&flag_arrows == 2)
		{
				OLED_Clear();
				OLED_Showwaiting();
				Send_Adjust();
				if (Tur >= 0) Tur_adjust -= Tur;
				Delay_ms(1000);
				OLED_Showfinish();
				flag_sure = 2;
				Delay_ms(1000);
				OLED_Clear();
		}
		else if(flag_sure == 1&flag_arrows == 3)
		{
			OLED_Clear();
			page_turner = 0;
			while(1)
			{
				ADD2 += page_turner*8;
				W25Q16_ReadData(ADD2, Savedata, 8);
				OLED_Showbase();//基础字符显示
				Tur = Savedata[0] ;
				Tur = (Tur << 8) + Savedata[1];
				OLED_ShowNum (1,8,Tur,4);
				OLED_ShowNum (2,8,Savedata[2],2);
				OLED_ShowNum (2,11,Savedata[3],2);
				OLED_ShowNum (3,11,Savedata[5],1);
				OLED_ShowNum (3,8,Savedata[4],2);
				OLED_ShowNum (4,8,Savedata[6]*16*16+Savedata[7],4);
				if (flag_sure == 2) break;
			}
			OLED_Clear();
		}
}


